package dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import dominio.Cliente;

public class ClienteDAO {
	private static String query_buscarCliente = "from Cliente c where c.cuil = (:cuil)";
	private static String query_listarClientes = "from Cliente c";

	private static ClienteDAO instancia = null;

	public static ClienteDAO getInstance() {
		if (instancia == null)
			instancia = new ClienteDAO();
		return instancia;
	}

	private ClienteDAO() {}

	public int guardarCliente(Cliente cliente) {
		Session s = HibernateDAO.getInstance().getSession();
		s.beginTransaction();
		s.saveOrUpdate(cliente);
		s.getTransaction().commit();
		return cliente.getId();
	}

	public Cliente buscarCliente(String cuil) {
		Session s = HibernateDAO.getInstance().getSession();
		Query query = s.createQuery(query_buscarCliente);
		query.setParameter("cuil", cuil);
		query.setMaxResults(1);
		return (Cliente) query.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<Cliente> listarClientes() {
		Session s = HibernateDAO.getInstance().getSession();
		return s.createQuery(query_listarClientes).list();
	}
}